﻿using System;
using System.Web.UI.Adapters;
using System.IO;
using System.Web;
using System.Web.UI;


namespace CNVP.Framework.URLRewriter.Form
{
    public class HtmlFormAdapter : ControlAdapter
    {
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(new FormRewriteTextWriter(writer));
        }
    }
    public class FormRewriteTextWriter : HtmlTextWriter
    {
        public FormRewriteTextWriter(TextWriter writer)
            : base(writer)
        {
            if (writer is HtmlTextWriter)
                this.InnerWriter = (writer as HtmlTextWriter).InnerWriter;
            else
                this.InnerWriter = writer;
        }

        public override void WriteAttribute(string name, string value, bool fEncode)
        {
            HttpContext context = HttpContext.Current;
            object rewroteAlready = context.Items["FormActionRewroteAlready"];
            if (name == "action" && rewroteAlready == null)
            {
                value = context.Request.RawUrl;
                context.Items["FormActionRewroteAlready"] = new object();
            }
            base.WriteAttribute(name, value, fEncode);
        }
    }
}